package com.hurui.xiaoming.activity.detailedlist;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.activity.BaseActivity;
import com.hurui.xiaoming.adapter.ViewPagerAdapter;
import com.hurui.xiaoming.fragment.detailedlist.DetailedListFragment;
import com.hurui.xiaoming.myconfig.PamaryKeys;

import java.util.ArrayList;
import java.util.List;

public class DetailedListActivity extends BaseActivity{

    private int select_page_num  ;

    private TabLayout tablayout ;
    private ViewPager viewPager ;
    private TextView top_title_tv;
    private List<Fragment> fragmentlists ;
    private List<String> titlelists ;
    private ViewPagerAdapter adapter ;

    private ImageView back_iv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailed_list);
        select_page_num = getIntent().getIntExtra(PamaryKeys.state_flags  , 0);

        initView() ;

        initTabs();

        initAction();

    }



    @Override
    public void initView() {
        tablayout = (TabLayout) findViewById(R.id.tablayout);

        viewPager = (ViewPager) findViewById(R.id.viewpager);

        top_title_tv = (TextView) findViewById(R.id.top_title_tv);

        top_title_tv.setText("我的清单");

        back_iv = (ImageView) findViewById(R.id.back_iv);

    }

    //初始化tab
    private void initTabs() {
        fragmentlists = new ArrayList<Fragment>();

        titlelists    = new ArrayList<String>() ;

        titlelists.add("全部");
        titlelists.add("借款中");
        titlelists.add("已还款");
        titlelists.add("已逾期");
        titlelists.add("已结清");

        fragmentlists.add(DetailedListFragment.newInstance("0"));
        fragmentlists.add(DetailedListFragment.newInstance("1"));
        fragmentlists.add(DetailedListFragment.newInstance("3"));
        fragmentlists.add(DetailedListFragment.newInstance("4"));
        fragmentlists.add(DetailedListFragment.newInstance("5"));

        adapter = new ViewPagerAdapter(getSupportFragmentManager()
                , this , fragmentlists , titlelists);

        tablayout.setTabMode(TabLayout.MODE_FIXED);


        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tablayout));

        tablayout.setupWithViewPager(viewPager);
        for (int i = 0; i < tablayout.getTabCount(); i++) {
            tablayout.getTabAt(i).setText(titlelists.get(i));
        }

        viewPager.setCurrentItem(select_page_num);
    }

    @Override
    public void initAction() {
        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onFinish();
            }
        });
    }
}
